/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.openmrs.module.basicmodule.DSS.Interpreter.Function.Time;

import org.openmrs.module.basicmodule.DSS.Interpreter.DSSObservation;
import org.openmrs.module.basicmodule.DSS.Interpreter.Function.Function;
import java.util.ListIterator;
import org.openmrs.module.basicmodule.DSS.Interpreter.Function.Function;
import java.util.ArrayList;
import org.openmrs.module.basicmodule.DSS.Interpreter.InterpreterVisitor;
import org.openmrs.module.basicmodule.DSS.ast.AST;
/**
 *
 * @author David
 */
public class OldestTimeItemFunction extends Function{

    @Override
    public Object execute(ArrayList args) {

           ArrayList<DSSObservation> d = (ArrayList<DSSObservation>) ((AST)args.get(0)).accept(new InterpreterVisitor());
//        String var = ((IdTree)args.get(0)).getSymbol().toString();
//        ArrayList<Observation> d = ((ArrayList)SymbolTable.Scope.getVarValue(var));
        ListIterator li = d.listIterator();

        DSSObservation currentOldest = d.get(0);
        DSSObservation dssob;
        while (li.hasNext()){
            dssob = (DSSObservation) li.next();
            if(dssob.getDate().after(currentOldest.getDate())) {
                currentOldest = dssob;
            }
        }
        
        
        return currentOldest;
    }
}
